package yt.kpool.activity.setting.agent.backwallet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.bean.BackWalletBean;
import yt.kpool.bean.PoolCoinBean;
import yt.kpool.bean.SetWalletAddress;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.pop.CalculatorAcPop;


/**
 * 设置钱包
 */
public class SetWalletAc extends AppCompatActivity {
    @BindView(R.id.edit_remark)
    EditText edit_remark;
    @BindView(R.id.edit_purse)
    EditText edit_purse;
    @BindView(R.id.image_down)
    ImageView image_down;
    @BindView(R.id.tv_coin)
    TextView tv_coin;
    private List<PoolCoinBean.DataBean.CoinListBean> listData;
    private CalculatorAcPop calculatorAcPop;
    private int currencyId;//币种id
    private LoadingDialog dialog;
    public static void start(Context mactivity, BackWalletBean backWalletBean) {
        Intent intent = new Intent(mactivity, SetWalletAc.class);
        Bundle bd=new Bundle();
        bd.putSerializable("bean",backWalletBean);
        intent.putExtra("bd",bd);
        mactivity.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_setwallet);
        ButterKnife.bind(this);
        initTitle();
        getNetData();
    }

    private void initTitle() {
        TitleControl t = new TitleControl(this);
        t.setTitle("设置钱包");

        dialog = new LoadingDialog(this, 0);
    }

    /**
     * 处理上层传过来的数据 显示币种选择默认第一条的信息
     */
    private void CreateData()
    {
        if(getBackWalletBean()==null||getBackWalletBean().getData().size()==0)
        {
            return;
        }
        else
        {
            showData(0,true);

        }

    }

    /**
     * b=true 首次默认显示的钱包信息 b=false 选择币种显示的钱包信息
     * @param m
     * @param b
     */
    private void showData(int m,boolean b)
    {
       if(b)
       {
             for (int i=0;i<listData.size();i++)
               {
                   if(getBackWalletBean().getData().get(m).getCurrencyID()==listData.get(i).getID())
                   {

                       setData(listData.get(i).getID(),listData.get(i).getCode().toUpperCase(),getBackWalletBean().getData().get(m).getRemark(),getBackWalletBean().getData().get(m).getAddress());
                   }
               }


       }
       else
       {
           if(getBackWalletBean().getData().size()==0)
           {

               setData(listData.get(m).getID(),listData.get(m).getCode().toUpperCase(),"","");
           }
           else
           {
               for (int i=0;i<getBackWalletBean().getData().size();i++)
               {
                   if(getBackWalletBean().getData().get(i).getCurrencyID()==listData.get(m).getID())
                   {

                       setData(listData.get(m).getID(),listData.get(m).getCode().toUpperCase(),getBackWalletBean().getData().get(i).getRemark(),getBackWalletBean().getData().get(i).getAddress());

                       break;
                   }
                   else
                   {
                       setData(listData.get(m).getID(),listData.get(m).getCode().toUpperCase(),"","");

                   }
               }
           }

       }

    }

    /**
     * 显示钱包信息
     * @param currencyId
     * @param coin
     * @param remark
     * @param address
     */
    private void setData(int currencyId,String coin,String remark,String address)
    {
        this.currencyId = currencyId;
        tv_coin.setText(coin);
        edit_remark.setText(remark);
        edit_purse.setText(address);
    }
//    private void setData(int currencyId,String coin)
//    {
//        this.currencyId = currencyId;
//        tv_coin.setText(coin);
//    }
    /**
     * 获取首页网络数据
     */
    private void getNetData() {
        dialog.show();
        NetHelperNew.PoolCoinList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                PoolCoinBean poolCoinBean = MyGson.getInstance().fromJson(data, PoolCoinBean.class);

                if (poolCoinBean.getType() != 1) {
                    Toast.makeText(SetWalletAc.this, poolCoinBean.getContent(), Toast.LENGTH_SHORT).show();
                } else {
                    listData = poolCoinBean.getData().getCoinList();
                    if (listData.size() > 0) {
                        CreateData();
                        calculatorAcPop = new CalculatorAcPop(SetWalletAc.this, listData, new CalculatorAcPop.OnSelectItemListener() {
                            @Override
                            public void select(int i) {
                                showData(i,false);
                            }
                        });

                        if(getBackWalletBean().getData().size()==0)
                        {
                            setData(listData.get(0).getID(),listData.get(0).getCode().toUpperCase(),"","");
                        }
                    }
                }

                dialog.dismiss();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(SetWalletAc.this, msg);
            }
        });

    }

    @OnClick({R.id.btn_submit, R.id.image_down})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit://确定

                setAddress();
                break;

            case R.id.image_down://币种选择
                if(calculatorAcPop!=null)
                    calculatorAcPop.showAsDropDown(image_down, 0, 0);
                break;
        }
    }

    private String getRemark() {
        return edit_remark.getText().toString();
    }

    private String getAddress() {
        return edit_purse.getText().toString();
    }


    /**
     * 接收上一页传来的Bean
     * @return
     */
    private BackWalletBean getBackWalletBean() {
        return (BackWalletBean)getIntent().getBundleExtra("bd").getSerializable("bean");
    }

    /**
     * 设置钱包地址
     */
    private void setAddress()
    {

        if(getAddress().isEmpty())
        {
            ToastUtil.showToast(SetWalletAc.this,"请填写结算钱包地址");
            return;
        }
        if(tv_coin.getText().toString().isEmpty())
        {
            ToastUtil.showToast(SetWalletAc.this,"请选择币种");
            return;
        }
        dialog.show();
        NetHelperNew.SetReturnWalletAddress(getAddress(), currencyId + "", getRemark(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                dialog.dismiss();
                SetWalletAddress setWalletAddress = MyGson.getInstance().fromJson(data, SetWalletAddress.class);
                if(setWalletAddress.getType()==1)
                {
                    EventBus.getDefault().post("5510");
                    finish();
                }

                ToastUtil.showToast(SetWalletAc.this,setWalletAddress.getContent());
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                dialog.dismiss();
                ToastUtil.showToast(SetWalletAc.this,msg);
            }
        });
    }
}
